/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.utl.ist.isr.livetrack.ui;

import java.awt.Dialog;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.text.MessageFormat;
import java.util.Map;
import javax.swing.Action;
import org.openide.DialogDisplayer;
import org.openide.WizardDescriptor;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.HelpCtx;
import org.openide.util.ImageUtilities;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;
import org.openide.util.actions.SystemAction;
import pt.utl.ist.isr.livetrack.server.LTServer;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class CamerasNode extends AbstractNode {

    private Action[] actions = new Action[]{
        new SystemAction() {

            @Override
            public String getName() {
                return NbBundle.getMessage(this.getClass(), "NewCamera");
            }

            @Override
            public HelpCtx getHelpCtx() {
                return null;
            }

            @Override
            public void actionPerformed(ActionEvent ev) {
                WizardDescriptor.Iterator iterator = new NewCameraWizardIterator(Utilities.actionsGlobalContext().lookup(LTServer.class));
                WizardDescriptor wizardDescriptor = new WizardDescriptor(iterator);
                // {0} will be replaced by WizardDescriptor.Panel.getComponent().getName()
                // {1} will be replaced by WizardDescriptor.Iterator.name()
                wizardDescriptor.setTitleFormat(new MessageFormat("{0} ({1})"));
                wizardDescriptor.setTitle("Your wizard dialog title here");
                Dialog dialog = DialogDisplayer.getDefault().createDialog(wizardDescriptor);
                dialog.setVisible(true);
                dialog.toFront();
                boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION;
                if (!cancelled) {
                    Map<String, Object> props = wizardDescriptor.getProperties();
                    if (props.get("type").equals("Axis")) {
                        server.getCameraFactory().newAxisCamera("" + props.get("id"), "" + props.get("hostname"));
                    } else if (props.get("type").equals("Sequential Files")) {
                        server.getCameraFactory().newSequentialFileCamera("" + props.get("id"), "" + props.get("directory"));
                    } else if (props.get("type").equals("Timestamped Files")) {
                        server.getCameraFactory().newTimestampedFileCamera("" + props.get("id"), "" + props.get("directory"));
                    } else {
                        throw new RuntimeException("Bug");
                    }
                    CamerasNodeChildFactory.getFactory(server).refresh();
                }
            }
        }};
    private LTServer server;

    public CamerasNode(LTServer server) {
        super(Children.create(CamerasNodeChildFactory.getFactory(server), false));
        this.server = server;
        setDisplayName(NbBundle.getMessage(this.getClass(), "CamerasNode_title"));
    }

    @Override
    public Action[] getActions(boolean context) {
        return actions;
    }

    public Image getIcon(int type) {
        return ImageUtilities.loadImage("pt/utl/ist/isr/livetrack/ui/icons/bullet2.png", true);
    }

    @Override
    public Image getOpenedIcon(int type) {
        return ImageUtilities.loadImage("pt/utl/ist/isr/livetrack/ui/icons/bullet2_down.png", true);
    }
}
